# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表，需要返回列表
    def printMatrix(self, matrix):
        # write code here
        w, h = len(matrix), len(matrix[0])
        res = []
        num = w * h
        cir_num = 0
        while len(res) < num:

            w_i = cir_num
            h_i = cir_num
            for i in range(h_i, h):
                res.append(matrix[w_i][i])

            h_i = h-1
            for j in range(w_i+1, w):
                res.append(matrix[j][h_i])

            w_i = w-1
            if len(res) == num:
                break
            for j in range(h-2, -1 + cir_num, -1):
                res.append(matrix[w_i][j])
            h_i = cir_num
            for i in range(w-2 , cir_num, -1):
                res.append(matrix[i][h_i])


            cir_num += 1
            w -= 1
            h -= 1

        return res

test = Solution()
print(test.printMatrix([[1,2,3,4],[5,6,7,8],[9,10,11,12]]))

